//
//  XBShopping.m
//  xunbao
//
//  Created by 程顺晓 on 16/4/4.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "XBShopping.h"
#import "AFHTTPSessionManager.h"
#import "NSObject+Model.h"
#import "XBList.h"
#import "XBTaglist.h"

@interface XBShopping ()

@end

@implementation XBShopping
/**
 *  加载新闻
 *
 *  @param success 成功回调
 *  @param failed  失败回调
 */
+ (void)loadNewsWithURLString:(NSString *)URLString success:(void (^)(NSMutableArray *headLines))success faild:(void (^)(NSError *error))failed {
//    NSLog(@"URLString = %@",URLString);
    NSAssert(success != nil, @"必须传人完成回调");
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:URLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary  *responseObject) {
        
        // 取得第一个主key
        NSString *keyMain = responseObject.keyEnumerator.nextObject;
        NSDictionary *dic = responseObject[keyMain];
        //        NSLog(@"%@",dic);
        NSArray *keys = dic.keyEnumerator.allObjects;
        NSString *firstKey = keys[1];
        NSArray *sourArr = dic[firstKey];
        
        //shopping模型数组
        NSArray *desArr = [self objectArrayWithDictArray:sourArr];
        

        
        
        
        
        NSMutableArray *shoppings = [NSMutableArray array];
        // 遍历循环拿数据
        [desArr enumerateObjectsUsingBlock:^(XBShopping *shopping, NSUInteger idx, BOOL * _Nonnull stop) {
            //XBShopping模型
            shopping = desArr[idx];
            //lists模型数组
            NSArray *lists = [XBList objectArrayWithDictArray:shopping.imgs];

            shopping.imgs = lists;
            [shoppings addObject:shopping];
            
        }];
        
        //成功回调
        success(shoppings.copy);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"错误---%@",error);
    }];
}


@end
